package com.luocj.muke.common.dialog.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.AnimatorRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.luocj.muke.common.R;
import com.luocj.muke.common.net.Utils;


public abstract class BaseDialog extends DialogFragment {
    private static final String MARGIN = "margin";
    private static final String WIDTH = "width";
    private static final String HEIGHT = "height";
    private static final String DIM = "dim";
    private static final String BOTTOM = "bottom";
    private static final String CANCELABLE = "cancelable";
    private static final String ANIM = "margin";
    private static final String LAYOUT = "layout";

    private int margin;
    private int width;
    private int height;
    private float dim = 0.5f;
    private boolean isShowBottom = false;
    private boolean cancelable = true;

    //动画
    @AnimatorRes
    private int animStyle;
    //布局
    @LayoutRes
    protected int layoutId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BaseDialog);
        layoutId = initLayouId();
        if (savedInstanceState != null) {
            margin = savedInstanceState.getInt(MARGIN);
            width = savedInstanceState.getInt(MARGIN);
            height = savedInstanceState.getInt(MARGIN);
            dim = savedInstanceState.getFloat(MARGIN);
            isShowBottom = savedInstanceState.getBoolean(BOTTOM);
            cancelable = savedInstanceState.getBoolean(CANCELABLE);
            animStyle = savedInstanceState.getInt(ANIM);
            layoutId = savedInstanceState.getInt(LAYOUT);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(layoutId, container, false);
        convertView(ViewHolder.creatView(inflate), this);
        return inflate;
    }

    @Override
    public void onStart() {
        super.onStart();
        initParams();
    }

    @SuppressLint("ResourceType")
    private void initParams() {
        Window window = getDialog().getWindow();
        if (null != window) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.dimAmount = dim;
            if (isShowBottom) {
                lp.gravity = Gravity.BOTTOM;
                if (animStyle == 0) {
                    animStyle = R.style.DeafaultAnim;
                }
            }

            if (width == 0) {
                lp.width = Utils.getScreenWidth(getContext()) - 2 * Utils.dip2px(getContext(), margin);
            } else if (width == -1) {
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            } else {
                lp.width = Utils.dip2px(getContext(), width);
            }

            if (height == 0) {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            } else {
                lp.height = Utils.dip2px(getContext(), height);
            }

            window.setWindowAnimations(animStyle);
            window.setAttributes(lp);

        }

        setCancelable(cancelable);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(MARGIN, margin);
        outState.putInt(WIDTH, width);
        outState.putInt(HEIGHT, height);
        outState.putFloat(DIM, dim);
        outState.putInt(ANIM, animStyle);
        outState.putBoolean(BOTTOM, isShowBottom);
        outState.putBoolean(CANCELABLE, cancelable);
        outState.putInt(LAYOUT, layoutId);
    }

    protected abstract void convertView(ViewHolder inflate, BaseDialog baseDialog);

    public abstract int initLayouId();

    public BaseDialog setMargin(int margin) {
        this.margin = margin;
        return this;
    }

    public BaseDialog setWidth(int width) {
        this.width = width;
        return this;
    }

    public BaseDialog setHeight(int height) {
        this.height = height;
        return this;
    }

    public BaseDialog setDim(float dim) {
        this.dim = dim;
        return this;
    }

    public BaseDialog setBottom(boolean isShowBottom) {
        this.isShowBottom = isShowBottom;
        return this;
    }

    public BaseDialog setOutcancelable(boolean cancelable) {
        this.cancelable = cancelable;
        return this;
    }

    public BaseDialog setAnim(int anim) {
        this.animStyle = anim;
        return this;
    }

    public BaseDialog show(FragmentManager fm) {
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        if (this.isAdded()) {
            fragmentTransaction.remove(this).commit();
        } else {
            fragmentTransaction.add(this, String.valueOf(System.currentTimeMillis()));
            fragmentTransaction.commitAllowingStateLoss();
        }
        return this;
    }
}
